<template>
    <div class="header">
        <section>
            <span>叩丁狼电商运营平台</span>
            <el-icon>
                <Grid />
            </el-icon>
        </section>

        <el-dropdown @command="clickCommand">
            <span class="el-dropdown-link">
                {{ useUser.username }}
                <el-icon class="el-icon--right">
                    <arrow-down />
                </el-icon>
            </span>
            <template #dropdown>
                <el-dropdown-menu>
                    <el-dropdown-item command="home">首页</el-dropdown-item>
                    <el-dropdown-item command="logout">退出</el-dropdown-item>
                </el-dropdown-menu>
            </template>
        </el-dropdown>
    </div>
</template>

<script setup lang="ts">
import { Grid, ArrowDown } from '@element-plus/icons-vue';
import { useUserStore } from "@/store"
import { useRouter } from 'vue-router';

const useUser = useUserStore()
const router = useRouter()

const clickCommand = (val: string) => {
    // console.log(val);
    if (val === "home") {
        router.push('/index')
    } else if (val === "logout") {
        // 删除token
        localStorage.removeItem("token")
        // 删除路由
        useUser.activeMenus.forEach((item) => {
            router.removeRoute(item.name)
        })
        router.removeRoute("home")
        // console.log(router.getRoutes());
        // 返回Login
        router.push("/login")
        // 清空pinia数据
        useUser.menus = []
        useUser.username = ""
    }
}

</script>

<style lang="less" scoped>
.header {
    text-align: left;
    display: flex;
    justify-content: space-between;
    color: #fff;
    align-items: center;
    height: 100%;

    section {
        span {
            font-weight: 400;
            font-size: 24px;
            padding: 0 20px;
        }

        .el-icon {
            font-size: 26px;
            vertical-align: middle;
        }
    }

    .el-dropdown {
        color: #fff;
        margin-right: 20px;
    }
}
</style>